package chances.epg.ui.taglib.model;

import java.awt.Point;
import java.util.HashMap;

import com.google.common.base.Strings;

/**
 * Class comments.
 *
 * @author <a href="mailto:tangfeng@chances.com.cn">tangfeng</a>
 */
public class Rect implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	private AlignEnum align;
	private AlignEnum valign;

	 int top;
	 int left;
	 int width;
	 int height;

	public Rect() {
		this.valign = AlignEnum.TOP;
		this.align = AlignEnum.LEFT;
	}

	public Rect(int pleft, int ptop, int pWidth, int pheight) {
		this.left = pleft;
		this.top = ptop;
		this.width = pWidth;
		this.height = pheight;
	}

	public int getTop() {
		return top;
	}

	public void setTop(int top) {
		this.top = top;
	}

	public int getLeft() {
		return left;
	}

	public void setLeft(int left) {
		this.left = left;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getStyle() {
		return "left:" + left + "px;top:" + top + "px;width:" + width
				+ "px;height:" + height + "px";

	}

	public boolean isEmpty() {
		return this.width + this.height == 0;
	}

	public String toString() {
		return getStyle();
	}

	public String getTopLeftSytle() {
		return "left:" + left + "px;top:" + this.top + "px";
	}

	public String getWidthHeightStyle() {
		return "width:" + width + "px;left:" + this.height + "px";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Rect other = (Rect) obj;
		if (height != other.height)
			return false;
		if (left != other.left)
			return false;

		if (top != other.top)
			return false;
		if (width != other.width)
			return false;
		return true;
	}

	public void add(Rect offset) {
		this.left = this.left + offset.left;
		this.top = this.top + offset.top;
		this.width = this.width + offset.width;
		this.height = this.height + offset.height;

	}

	public static Rect parseStyle(String rectStyle) {
		if (Strings.isNullOrEmpty(rectStyle)) {
			return new Rect();
		}
		String[] styles = rectStyle.split(";");
		HashMap<String, String> keyvalues = new HashMap<String, String>();
		for (String attrs : styles) {
			String[] keyValue = attrs.split(":");
			if (keyValue.length > 1) {
				keyvalues.put(keyValue[0].trim(), keyValue[1].trim());
			}
		}
		String align = keyvalues.get("align");
		String valign = keyvalues.get("valign");
		if (Strings.isNullOrEmpty(align)) {
			align = AlignEnum.LEFT.getName();
		}
		if (Strings.isNullOrEmpty(valign)) {
			valign = AlignEnum.TOP.getName();
		}

		int width = parseInt(keyvalues.get("width"));
		int top = parseInt(keyvalues.get("top"));
		int left = parseInt(keyvalues.get("left"));
		int height = parseInt(keyvalues.get("height"));
		Rect result = new Rect(left, top, width, height);
		result.setAlign(AlignEnum.findAlign(align));
		result.setValign(AlignEnum.findValign(valign));
		return result;

	}

	public Rect getChildRect(Rect tagRect, boolean topleft) {
		Rect result = new Rect();
		result.setWidth(tagRect.width);
		result.setHeight(tagRect.height);
		result.setLeft(0);
		result.setTop(0);
		switch (tagRect.getAlign()) {
		case LEFT:
			result.setLeft(0);	
			break;
		case RIGHT:
			result.setLeft(this.width - tagRect.width);
			break;
		case CENTER:
			result.setLeft((this.width - tagRect.width) / 2);
			break;
		default:
			break;
		}
		switch (tagRect.getValign()) {
		case TOP:
			result.setTop(0);
			break;
		case BOTTOM:
			result.setTop(this.height - tagRect.height);
			break;
		case MIDDLE:
			result.setTop((this.height - tagRect.height) / 2);
			break;
		default:
			break;
		}
		if (topleft) {
			result.setTop(result.top + tagRect.top);
			result.setLeft(result.left + tagRect.left);
		}
		if(result.getWidth()==0){
			result.setWidth(this.getWidth());
		}
		if(result.getHeight()==0){
			result.setHeight(this.getHeight());
		}
		return result;

	}

	public static int parseInt(String string) {
		if (Strings.isNullOrEmpty(string)) {
			return 0;
		}
		String strValue = string.replace("px", "");
		if (!Strings.isNullOrEmpty(strValue)) {
			try {
				float value = Float.parseFloat(strValue);
				return (int) value;
			} catch (java.lang.Throwable e) {
				return 0;
			}
		} else {
			return 0;
		}
	}

	public AlignEnum getAlign() {
		return align;
	}

	public AlignEnum getValign() {
		return valign;
	}

	public void setAlign(AlignEnum align) {
		this.align = align;
	}

	public void setValign(AlignEnum valign) {
		this.valign = valign;
	}

	public void copyto(Rect aRect) {
		aRect.left = this.left;
		aRect.height = this.height;
		aRect.width = this.width;
		aRect.top = this.top;
		aRect.align = this.align;
		aRect.valign = this.valign;
	}

	public void addBorder(int border) {
		this.top = this.top - border;
		this.left = this.left - border;
		this.width = this.width + border * 2;
		this.height = this.height + border * 2;
	}

	public void addBorder(Border border) {
		if (border != null) {
			this.top = this.top - border.getTop();
			this.left = this.left - border.getLeft();
			this.width = this.width + border.getRight();
			this.height = this.height + border.getBottom();
		}
	}

	public void addTopLeft(Rect r) {
		this.top = this.top + r.getTop();
		this.left = this.left + r.getLeft();

	}
	
	public Point getCenterPoint(){
		int cwidth = this.width / 2;
		int cheight = this.height / 2;
		Point point = new Point(left + cwidth, top + cheight);
		return point;
	}

	public void subduction(Border padding) {
		if (padding != null) {
			this.top = this.top + padding.getTop();
			this.left = this.left + padding.getLeft();
			this.width = this.width - padding.getRight();
			this.height = this.height - padding.getBottom();
		}
	}
	
}
